<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频点播系统 - 登录与注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

       .main-container {
            background-color: rgba(255, 255, 255, 0.95);
            padding: 30px 80px;
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2), 0 6px 6px rgba(0, 0, 0, 0.25);
            width: 350px;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

       .title {
            font-family: "STXingkai", "KaiTi", cursive;
            font-size: 48px;
            text-align: center;
            color: #000;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
            margin-bottom: 20px;
            transition: transform 0.3s ease;
        }

       .title:hover {
            transform: scale(1.1);
        }

       .tab-buttons {
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
            border-bottom: 1px solid #ccc;
        }

       .tab-buttons button {
            background: none;
            border: none;
            font-size: 20px;
            cursor: pointer;
            padding: 10px 20px;
            border-bottom: 2px solid transparent;
            transition: all 0.3s ease;
            color: #333;
        }

       .tab-buttons button:hover {
            transform: scale(1.1);
            color: #007BFF;
        }

       .tab-buttons button.active {
            border-bottom-color: #007BFF;
            color: #007BFF;
        }

       .form-group {
            margin-bottom: 20px;
        }

       .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }

       .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

       .form-group input:focus {
            outline: none;
            border-color: #007BFF;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
        }

       .form-group button {
            width: 100%;
            padding: 14px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 18px;
            transition: background-color 0.3s ease, transform 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

       .form-group button:hover {
            background-color: #0056b3;
            transform: scale(1.05);
        }

       .tab-content {
            display: none;
        }

       .tab-content.active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="main-container">
        <div class="title">视频点播</div>
        <div class="tab-buttons">
            <button id="login-tab" class="active">登录</button>
            <button id="register-tab">注册</button>
        </div>
        <div id="login-content" class="tab-content active">
            <form id="login-form">
                <div class="form-group">
                    <label for="login-username">用户名</label>
                    <input type="text" id="login-username" required>
                </div>
                <div class="form-group">
                    <label for="login-password">密码</label>
                    <input type="password" id="login-password" required>
                </div>
                <div class="form-group">
                    <button type="submit">登录</button>
                </div>
            </form>
        </div>
        <div id="register-content" class="tab-content">
            <form id="register-form">
                <div class="form-group">
                    <label for="register-username">用户名</label>
                    <input type="text" id="register-username" required>
                </div>
                <div class="form-group">
                    <label for="register-password">密码</label>
                    <input type="password" id="register-password" required>
                </div>
                <div class="form-group">
                    <button type="submit">注册</button>
                </div>
            </form>
        </div>
    </div>
    <script>
        document.getElementById('login-tab').addEventListener('click', function () {
            document.getElementById('login-tab').classList.add('active');
            document.getElementById('register-tab').classList.remove('active');
            document.getElementById('login-content').classList.add('active');
            document.getElementById('register-content').classList.remove('active');
        });

        document.getElementById('register-tab').addEventListener('click', function () {
            document.getElementById('register-tab').classList.add('active');
            document.getElementById('login-tab').classList.remove('active');
            document.getElementById('register-content').classList.add('active');
            document.getElementById('login-content').classList.remove('active');
        });

        document.getElementById('login-form').addEventListener('submit', async function (e) {
            e.preventDefault();
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;

            if (!username || !password) {
                alert('请输入用户名和密码');
                return;
            }

            try {
                const response = await fetch('/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username: username, password: password })
                });

                const data = await response.json();
                if (data.result) {
                    alert('登录成功！');
                    // 存储用户名到本地存储
                    localStorage.setItem('username', username);
                    // 登录成功后跳转到 list.html 界面
                    window.location.href = '/list.html';
                } else {
                    alert(data.reason);
                }
            } catch (error) {
                alert('网络错误，请重试：' + error.message);
            }
        });

        document.getElementById('register-form').addEventListener('submit', async function (e) {
            e.preventDefault();
            const username = document.getElementById('register-username').value;
            const password = document.getElementById('register-password').value;

            if (!username || !password) {
                alert('请输入用户名和密码');
                return;
            }

            try {
                const response = await fetch('/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username: username, password: password })
                });

                const data = await response.json();
                if (data.result) {
                    alert('注册成功！');
                    // 注册成功后自动切换到登录页
                    document.getElementById('login-tab').click();
                } else {
                    alert(data.reason);
                }
            } catch (error) {
                alert('网络错误，请重试：' + error.message);
            }
        });
    </script>
</body>

</html>